package com.sh.sales.happysales.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sh.polymedicine.happysale.result.SimpleResult;
import com.sh.polymedicine.happysale.service.envelop.HappySaleManager;
import com.sh.sales.happysales.R;
import com.sh.sales.happysales.app.AppData;
import com.sh.sales.happysales.app.utils.LoginOutUtil;
import com.sh.sales.happysales.base.BaseActivity;
import com.sh.sales.happysales.service.LoginObsever;
import com.sh.sales.happysales.view.NewToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by zhouyucheng on 2017/9/15.
 */

public class FeedbackActivity extends BaseActivity implements LoginObsever {
    @BindView(R.id.set_back)
    ImageView setBack;
    @BindView(R.id.profect_next)
    TextView profectNext;
    @BindView(R.id.feedback_content)
    EditText feedbackContent;
    @BindView(R.id.feedback_connect)
    EditText feedbackConnect;
    @BindView(R.id.feedback_btn)
    Button feedbackBtn;
    private Subscriber<SimpleResult> feedBackSubscriber;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        mAppManager.addActivity(this);
    }

    @OnClick({R.id.set_back, R.id.feedback_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.set_back:
                mAppManager.finishActivity(this);
                break;
            case R.id.feedback_btn:
                feedbackCommit();
                break;
        }
    }

    private void feedbackCommit() {
        String feedbacktext = feedbackContent.getText().toString().trim();
        String feedbackconnect = feedbackConnect.getText().toString().trim();
        String userSign = AppData.getInstence().getmUserToken().getUserSign();
        if ("".equals(feedbackconnect)) {
            NewToast.makeText(FeedbackActivity.this, "反馈内容不能为空");
            return;
        }
        if (feedBackSubscriber != null) {
            if (!feedBackSubscriber.isUnsubscribed()) {
                feedBackSubscriber.unsubscribe();
            }
        }
        feedBackSubscriber = new Subscriber<SimpleResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.i("FeedbackActivity", "onError: " + e);
                if (HappySaleManager.DATA_FAIL.equals(e.getMessage())) {
                    String message = e.getCause().getCause().getMessage();
                    NewToast.makeText(FeedbackActivity.this, message);
                    int ret =Integer.valueOf( e.getCause().getMessage()) ;
                    if (ret>=100&&ret<200){
                        LoginOutUtil.logOut(FeedbackActivity.this);
                    }
                } else if (HappySaleManager.DATA_NULL.equals(e.getMessage())) {
                    NewToast.makeText(FeedbackActivity.this, e.getCause().getMessage());
                } else {
                    NewToast.makeText(FeedbackActivity.this, "意见反馈失败，请检查网络");
                }
            }

            @Override
            public void onNext(SimpleResult mSimpleResult) {
                NewToast.makeText(FeedbackActivity.this, "反馈提交成功");
                mAppManager.finishActivity(FeedbackActivity.this);
            }
        };

        mHappySaleManager.feedback(feedbacktext, feedbackconnect, userSign, feedBackSubscriber).subscribe(feedBackSubscriber);
    }

    @Override
    public void refreshUserSign(int Position) {
        feedbackCommit();
    }

    @Override
    protected void onDestroy() {
        mLoginObserable.unregisterObserver(this);
        super.onDestroy();
    }
}
